package com.sl.au.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Protype;
import com.sl.au.service.GetProductChartService;
import com.sl.utils.ExdeUtil;

@RestController
@RequestMapping(value = "/ui/productInfo")
public class ProductInfoController extends BaseController {
	
	@Autowired
	GetProductChartService getProductChartService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("productTotal/productInfo");
		return modelAndView;
	}
	
	/***
	 * 获取大类型分析饼图
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "largeType", method = RequestMethod.GET)
	public Map<String, Object> getBigTypeChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String a = request.getParameter("beginDate");
		String b = request.getParameter("endDate");
		String c =  request.getParameter("type");
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		List list = getProductChartService.getProductChartsService(0, request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("type"));
		List<Protype> returnList = new ArrayList<Protype>();
		for (Object row : list) {  
	        Object[] cells = (Object[]) row;
	        Protype protype = new Protype();
	        protype.setValue(Float.parseFloat(cells[1].toString()));
	        protype.setName(cells[0].toString());
	        returnList.add(protype);
	    }
		map.put("data", returnList);
		return map;
	}
	
	/***
	 * 获取细分类型分析饼图
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "smallType", method = RequestMethod.GET)
	public Map<String, Object> getSmallTypeChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String a = request.getParameter("beginDate");
		String b = request.getParameter("endDate");
		String c =  request.getParameter("type");
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		List list = getProductChartService.getProductChartsService(1, request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("type"));
		List<Protype> returnList = new ArrayList<Protype>();
		for (Object row : list) {  
	        Object[] cells = (Object[]) row;
	        Protype protype = new Protype();
	        protype.setValue(Float.parseFloat(cells[1].toString()));
	        protype.setName(cells[0].toString());
	        returnList.add(protype);
	    }
		map.put("data", returnList);
		return map;
	}
	
	/***
	 * 趋势图
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getTrendChart", method = RequestMethod.GET)
	public Map<String, Object> getTrendChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String isAll = request.getParameter("isAll").toString();
		String s = request.getParameter("type");
		List listTotalSum = null;
		List CTlistTotalSum = null;
		if(isAll.equals("大类型")){
			//平均价
			listTotalSum= getProductChartService.getProductChartsService(2,request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("type"));
			//总价
			CTlistTotalSum= getProductChartService.getProductChartsService(3,request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("type"));
		}else{
			//平均价
			listTotalSum= getProductChartService.getProductChartsService(4,request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("type"));
			//总价
			CTlistTotalSum= getProductChartService.getProductChartsService(5,request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("type"));
		}
		List<Float> seriesSumTotal = new ArrayList<Float>();//获取系统所有客户（时间范围内）
		List<Float> CTseriesSumTotal = new ArrayList<Float>();//获取时间范围内成功的客户
		List<String> Axis = new ArrayList<String>();//横坐标
		//获取系统所有客户（时间范围内）
		try {
			Date d1 = new SimpleDateFormat("yyyy-MM").parse(request.getParameter("beginDate"));
			Date d2 = new SimpleDateFormat("yyyy-MM").parse(request.getParameter("endDate"));//定义结束日期
			Calendar dd = Calendar.getInstance();//定义日期实例
			Calendar dd2 = Calendar.getInstance();//定义日期实例
			dd.setTime(d1);
			dd2.setTime(d2);
			dd2.add(Calendar.MONTH, 1);//进行当前日期月份加1
			while(dd.getTime().before(dd2.getTime())){//判断是否到结束日期
				boolean isFind = false;
				for(Object row : listTotalSum){
					Object[] cells = (Object[]) row;
					if(cells[0].toString().equals(ExdeUtil.datechange(dd.getTime(), "yyyy-MM"))){
						seriesSumTotal.add(Float.parseFloat(cells[1].toString()));
						isFind = true;
						break;
					}
				}
				if(!isFind){
					seriesSumTotal.add(new Float(0));
				}
				dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		////获取时间范围内成功的客户
		try {
			Date d1 = new SimpleDateFormat("yyyy-MM").parse(request.getParameter("beginDate"));
			Date d2 = new SimpleDateFormat("yyyy-MM").parse(request.getParameter("endDate"));//定义结束日期
			Calendar dd = Calendar.getInstance();//定义日期实例
			Calendar dd2 = Calendar.getInstance();//定义日期实例
			dd.setTime(d1);
			dd2.setTime(d2);
			dd2.add(Calendar.MONTH, 1);//进行当前日期月份加1
			while(dd.getTime().before(dd2.getTime())){//判断是否到结束日期
				boolean isFind = false;
				for(Object row : CTlistTotalSum){
					Object[] cells = (Object[]) row;
					if(cells[0].toString().equals(ExdeUtil.datechange(dd.getTime(), "yyyy-MM"))){
						CTseriesSumTotal.add(Float.parseFloat(cells[1].toString()));
						isFind = true;
						break;
					}
				}
				if(!isFind){
					CTseriesSumTotal.add(new Float(0));
				}
				Axis.add(dd.get(Calendar.YEAR) +"年" + (dd.get(Calendar.MONTH)+1) + "月");
				dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		map.put("Axis", Axis);
		map.put("seriesSumTotal", seriesSumTotal);
		map.put("CTseriesSumTotal", CTseriesSumTotal);
		return map;
	}
	/***
	 * 获取行业柱状图
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getChanceDeptChart", method = RequestMethod.GET)
	public Map<String, Object> getChanceDeptChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String isAll = request.getParameter("isAll").toString();
		List listTotalSum = null;
		List CTlistTotalSum = null;
		String s = request.getParameter("type");
		if(isAll.equals("大类型")){
			//平均价
			listTotalSum= getProductChartService.getProductChartsService(6,request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("type"));
			//总价
			CTlistTotalSum= getProductChartService.getProductChartsService(7,request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("type"));
		}else{
			//平均价
			listTotalSum= getProductChartService.getProductChartsService(8,request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("type"));
			//总价
			CTlistTotalSum= getProductChartService.getProductChartsService(9,request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("type"));
		}
		List<Float> series = new ArrayList<Float>();//平均价
		List<Float> series2 = new ArrayList<Float>();//总价
		List<String> Axis = new ArrayList<String>();
			for (Object row : listTotalSum) {  
				Object[] cells = (Object[]) row;
		        Axis.add(cells[0].toString());
		        series.add(Float.parseFloat(cells[1].toString()));
		    }
			for (Object row : CTlistTotalSum) {  
				Object[] cells = (Object[]) row;
				series2.add(Float.parseFloat(cells[1].toString()));
		    }
		map.put("Axis", Axis);
		map.put("series", series);
		map.put("series2", series2);
		return map;
	}
	
	
}
